#include <iostream>
#include <string>
#include <cmath>

using std::cout;
using std::endl;
using std::string;
using std::sqrt;

class Figure {
public:
    virtual string getName() const = 0;
    virtual double getArea() const = 0;
};

class Rectangle : public Figure{
public:
    Rectangle(int length = 0, int width = 0);
    string getName() const override;
    double getArea() const override;
    
private:
    int _length, _width;
};

Rectangle::Rectangle(int length, int width)
    :_length(length)
    ,_width(width)
{}
string Rectangle::getName() const{
    //cout << "Rectangle";
    return "Rectangle";
}
double Rectangle::getArea() const{
    //cout << _length * _width;
    return _length * _width;
}

class Circle : public Figure {
public:
    Circle(double radius = 0);
    string getName() const override;
    double getArea() const override;
private:
    double _radius;
};

Circle::Circle(double radius) 
    : _radius(radius) 
{}

string Circle::getName() const {
    return "圆形";
}

double Circle::getArea() const {
    return 3.14159 * _radius * _radius; // πr²
}

class Triangle : public Figure {
public:
    Triangle(double a, double b, double c);
    string getName() const override;
    double getArea() const override;
private:
    double _a, _b, _c;
};

Triangle::Triangle(double a, double b, double c)
    : _a(a), _b(b), _c(c) {}

string Triangle::getName() const {
    return "三角形";
}

double Triangle::getArea() const {
    // 海伦公式计算面积
    double p = (_a + _b + _c) / 2.0;
    double ret = sqrt((double)p * (p * _a) * (p - _b) * (p - _c));
    return ret;
}


void display(Figure & fig) {
    cout << fig.getName() 
         << "的面积是:" 
         << fig.getArea() << endl ;
}

void test() {
    Rectangle r(10, 20);
    Circle c(15);
    Triangle t(3, 4, 5);
    display(r);
    display(c);
    display(t);
}

int main()
{
    test();
    return 0;
}

